//
//  PlayView.m
//  27-BlockTest-PlayButton
//
//  Created by Snow on 16/8/19.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
@interface PlayView ()
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, copy) void (^play)(UIButton *);
@property (nonatomic, copy) void (^pause)(UIButton *);
@property (nonatomic, copy) void (^last)(UIButton *);
@property (nonatomic, copy) void (^next)(UIButton *);
@end

@implementation PlayView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup{
    _playButton = [self buttonImageWithName:@"hp_player_btn_play_highlight" action:@selector(playButton:)];
    _leftButton = [self buttonImageWithName:@"hp_player_btn_pre_highlight" action:@selector(lastButton:)];
    _rightButton = [self buttonImageWithName:@"hp_player_btn_next_normal" action:@selector(nextButton:)];
    
    [self addSubview:_rightButton];
    [self addSubview:_leftButton];
    [self addSubview:_playButton];
}
- (void)lastButton:(UIButton *)button
{
    self.backgroundColor = [UIColor redColor];
    self.last(_leftButton);
}
- (void)nextButton:(UIButton *)button
{
    self.backgroundColor = [UIColor yellowColor];
    self.next(_rightButton);
}
- (void)playButton:(UIButton *)button
{
    [button setImage:[UIImage imageNamed:@"hp_player_btn_pause_highlight"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(pauseButton:) forControlEvents:UIControlEventTouchUpInside];
    self.play(_playButton);
}
- (void)pauseButton:(UIButton *)button
{
    [button setImage:[UIImage imageNamed:@"hp_player_btn_play_highlight"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(pauseButton:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(_playButton);
}
- (UIButton *)buttonImageWithName:(NSString *)imageName action:(SEL)action
{
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _playButton.frame = CGRectMake(0, 0, 100, 100);
    _playButton.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    _leftButton.x_width = 60;
    _leftButton.x_height = 60;
    _leftButton.x_centerY = _playButton.x_centerY;
    _leftButton.x_conterX = _playButton.x_conterX - (_leftButton.x_width / 2 + _playButton.x_width / 2);
    
    _rightButton.x_width = 60;
    _rightButton.x_height = 60;
    _rightButton.x_centerY = _playButton.x_centerY;
    _rightButton.x_conterX = _playButton.x_conterX + (_rightButton.x_width / 2 + _playButton.x_width / 2);
}
- (void)play:(void (^)(UIButton *play))play pause:(void (^)(UIButton *pause))pause last:(void (^)(UIButton *))last next:(void (^)(UIButton *))next
{
    self.play = play;
    self.pause = pause;
    self.last = last;
    self.next = next;

    
}
@end
